<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="仓库" prop="warehouseId">
        <el-select v-model="queryParams.warehouseId" placeholder="请选中仓库" clearable filterable @change="warehouseChange" @keyup.enter.native="handleQuery">
          <el-option v-for="item in warehouseList" :key="item.warehouseId" :label="item.warehouseName" :value="item.warehouseId" />
        </el-select>
      </el-form-item>

      <el-form-item label="库位" prop="areaId">
        <el-select v-model="queryParams.areaId" placeholder="请选中库位" clearable filterable @keyup.enter.native="handleQuery">
          <el-option v-for="item in areaList" :key="item.areaId" :label="item.areaCode+'['+item.attr3+']'" :value="item.areaId" />
        </el-select>
      </el-form-item>
      <el-form-item label="物料编码" prop="itemCode">
        <el-input v-model="queryParams.itemCode" placeholder="请输入物料编码" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" v-hasPermi="['wms:stock:export']">导出</el-button>
        <!-- <el-button icon="el-icon-refresh" size="mini" @click="download">重置</el-button> -->
        <el-button icon="el-icon-refresh" size="mini" type="primary" :loading="isLoading" @click="synErpInventory">
          {{ isLoading ? '同步中' : '数据同步' }}
        </el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="wmstockList" @selection-change="handleSelectionChange" height="calc(100vh - 230px)">
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <el-table-column type="index" label="序号" align="center" width="50" />
      <el-table-column label="仓库" width="100px" align="center" prop="warehouseName" />
      <el-table-column label="库位" width="100px" align="center" prop="areaName" />
      <el-table-column label="物料编码" width="160px" align="center" prop="itemCode" />
      <el-table-column label="物料名称" width="200px" align="center" prop="itemName" />
      <el-table-column label="规格型号" align="center" prop="specification" />
      <el-table-column label="在库数量" width="100px" align="center" prop="number" />
      <el-table-column label="单位" width="100px" align="center" prop="unitOfMeasure" />
      <!-- <el-table-column label="入库批次号" width="120px" align="center" prop="batchCode" /> -->
      <!-- <el-table-column label="库区" align="center" prop="locationName" />
        <el-table-column label="库位" align="center" prop="areaName" />
        <el-table-column label="供应商编号" width="100px" align="center" prop="vendorCode" />
        <el-table-column label="供应商名称" width="120px" align="center" prop="vendorName" :show-overflow-tooltip="true"/>
        <el-table-column label="供应商简称" width="100px" align="center" prop="vendorNick" />
        <el-table-column label="生产工单" width="100px" prop="workorderCode" /> -->
      <!-- <el-table-column label="入库日期" align="center" prop="recptDate" width="120">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.recptDate, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="库存有效期" align="center" prop="expireDate" width="120">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.expireDate, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column> -->
      <!-- <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" v-if="scope.row.isKeyItem==0" v-hasPermi="['wm:itemrecptline:itemkeySet']" @click="addMdItemKey(scope.row)">设为关键器件</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" v-if="scope.row.isKeyItem == 1 && hasPerm('wms:stock:deleteKeyItem')" @click=" delMdItemKey(scope.row)">解绑</el-button>
        </template>
      </el-table-column> -->
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.current" :limit.sync="queryParams.size" @pagination="getList" />
    <!-- </el-col>
    </el-row> -->
  </div>
</template>

<script>
import { list, synErpInventory } from "@/api/mes/wm/wmstock";
import { treeselect } from "@/api/mes/md/itemtype";
import Treeselect from "@riophae/vue-treeselect";
import { listWarehouse } from "@/api/mes/wm/warehouse";
import { listArea } from "@/api/mes/wm/area"
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
export default {
  name: "Wmstock",
  components: { Treeselect },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      itemTypeName: null,
      areaList: [],
      // 总条数
      total: 0,
      //物料产品分类树
      itemTypeOptions: undefined,
      // 库存记录表格数据
      wmstockList: [],
      warehouseList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        current: 1,
        size: 10,
        itemTypeId: null,
        itemId: null,
        itemCode: null,
        itemName: null,
        specification: null,
        unitOfMeasure: null,
        measureName: null,
        batchCode: null,
        warehouseId: null,
        warehouseCode: null,
        warehouseName: null,
        locationId: null,
        locationCode: null,
        locationName: null,
        areaId: null,
        areaCode: null,
        areaName: null,
        vendorId: null,
        vendorCode: null,
        vendorName: null,
        vendorNick: null,
        quantityOnhand: null,
        workorderCode: null,
        expireDate: null,
      },
      // 表单参数
      form: {},
      isLoading: false
    };
  },

  created() {
    this.getList();
    this.getWarehouseList();
    // this.getAreaList();
  },
  computed: {
    perms() {
      //获取当前登录人的权限数组
      return this.$store.getters.permissions || [];
    }
  },
  methods: {
    hasPerm(perm) {
      //判断当前登录人是否拥有解绑权限，perm为按钮上设置的权限字符。this.perms为当前登录人的权限组
      //JavaScript 的includes方法，用来判断一个数组中是否包含某个值。
      //如果当前登录人是超级管理员，那么也拥有权限
      //*:*:*为超级管理员的权限
      return this.perms.includes('*:*:*') || this.perms.includes(perm);
    },
    //绑定关键器件
    addMdItemKey(data) {
      let prams = {
        "itemCode": data.itemCode,
        "itemName": data.itemName,
        "specification": data.specification,
        "unitOfMeasure": data.unitOfMeasure
      }
      itemkeySet(prams).then(msg => {
        console.log(msg);
        if (msg.code == 200) {
          this.$modal.msgSuccess("设置成功");
          this.getList()
        }
      })
    },
    //解绑
    delMdItemKey(data) {
      deleteKeyItem(data.itemCode).then(msg => {
        if (msg.code == 200) {
          this.$modal.msgSuccess("解绑成功");
          this.getList()
        }
      })
    },
    // 数据同步按钮控制
    synErpInventory() {
      this.isLoading = true
      let self = this
      synErpInventory().then(msg => {
        self.isLoading = false
        this.$message(msg.msg);
      }).catch(erro => {
        console.log(erro);
        this.isLoading = false
      })
    },
    warehouseChange() {
      this.getAreaList();
      this.queryParams.areaId = null;
    },
    getWarehouseList() {
      listWarehouse({ pageSize: 999 }).then(response => {
        this.warehouseList = response.rows
      })
    },
    getAreaList() {
      listArea({ wareHouseId: this.queryParams.warehouseId }, { "no-page": true }).then(response => {
        this.areaList = response.rows
      })
    },
    /** 查询库存记录列表 */
    getList() {
      this.loading = true;
      list(this.queryParams).then(response => {
        this.wmstockList = response.data.records
        this.total = response.data.total;
        this.loading = false;
      });
    },

    // 节点单击事件
    handleNodeClick(data) {
      this.queryParams.itemTypeId = data.id;
      this.handleQuery();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.materialStockId)
      this.queryParams.wmMaterialStockIdList = selection.map(item => item.materialStockId);
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('mes/wm/warehouse/inventory/export', {
        ...this.queryParams
      }, `wmstock_${new Date().getTime()}.xlsx`, null, "get")
    }
  }
};
</script>
